package com.bluesky.taskDemo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 简单的任务批处理
 */
public class SimpleBatchTask {


    public static void main(String[] args) {
            batchTaskTest();
    }

    /**
     * 使用线程池批量发送短信，发送完毕后，方法继续向下走
     */
    public static void batchTaskTest() {

        long startTime = System.currentTimeMillis();
        //待发送的短信列表
        List<String> taskList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            taskList.add("短信-"+i);
        }

        //使用线程池批量处理任务
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //创建CountDownLatch ,构造器参数为任务数量
        CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
        for (String task : taskList) {
            executorService.execute(() -> {
                try {
                    //模拟发送短信耗时
                    disposeTask(task);
                  /*  Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+" 发送短信："+task);*/
                } finally {
                    //任务执行完毕，计数器减一
                    countDownLatch.countDown();
                }
           });
        }


        try {
            //阻塞当前线程池
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("任务处理完毕，总耗时："+(System.currentTimeMillis()-startTime));
        //关闭线程池
        executorService.shutdown();
    }

    public static void disposeTask(String task){
        System.out.println(String.format("【%s】发送成功！", task));
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
